package com.seawaterbt.ssm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seawaterbt.ssm.entity.*;
import com.seawaterbt.ssm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ZhoneHeatingExceptionInfo {
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    @Autowired
    ZHeatingExceptionInfoService zHeatingExceptionInfoService;
    @Autowired
    IHeatingExceptionInfoService heatingExceptionInfoService;
    @Autowired CzBasHeatcompanyService czBasHeatcompanyService;

    public void pull(){
        QueryWrapper<ZHeatingExceptionInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isOther",1).eq("wdtIsSync",0);
        List<ZHeatingExceptionInfo> zHeatingExceptionInfoList = zHeatingExceptionInfoService.list(queryWrapper);
        if (zHeatingExceptionInfoList != null){
            for (ZHeatingExceptionInfo zHeatingExceptionInfo:zHeatingExceptionInfoList){
                QueryWrapper<HeatingExceptionInfo> heatingExceptionInfoQueryWrapper = new QueryWrapper<>();
                heatingExceptionInfoQueryWrapper.eq("code_city",zHeatingExceptionInfo.getAbnormalCityID());
                HeatingExceptionInfo heatingExceptionInfo = heatingExceptionInfoService.getOne(heatingExceptionInfoQueryWrapper);
                if (heatingExceptionInfo == null){
                    heatingExceptionInfo = new HeatingExceptionInfo();
                }
                QueryWrapper<BasHeatcompany> heatcompanyQueryWrapper = new QueryWrapper<>();
                heatcompanyQueryWrapper.like("heat_company_name",zHeatingExceptionInfo.getHeatCompanyName());
                BasHeatcompany basHeatcompany = czBasHeatcompanyService.getOne(heatcompanyQueryWrapper);
                heatingExceptionInfo.setAddress(zHeatingExceptionInfo.getLocation());
                heatingExceptionInfo.setArea(zHeatingExceptionInfo.getRegionID());
                heatingExceptionInfo.setCloseTime(zHeatingExceptionInfo.getRepairTime());
                heatingExceptionInfo.setCaseDescription(zHeatingExceptionInfo.getContent());
                heatingExceptionInfo.setCodeCity(zHeatingExceptionInfo.getAbnormalCityID());
                heatingExceptionInfo.setCompanyCode(basHeatcompany.getCode());
                heatingExceptionInfo.setComplainLevel(1);//默认企业上报
                heatingExceptionInfo.setContact(zHeatingExceptionInfo.getPeopleName());
                heatingExceptionInfo.setDisposalDescription(zHeatingExceptionInfo.getProgress());
                String type = "异常";
                if (zHeatingExceptionInfo.getType()==0){
                    type = "隐患";
                }else if (zHeatingExceptionInfo.getType()==1){
                    type = "故障";
                }
                heatingExceptionInfo.setExceptionType(type);
                heatingExceptionInfo.setImpactArea(zHeatingExceptionInfo.getAffectArea()+"");
                heatingExceptionInfo.setImpactHouseCount(zHeatingExceptionInfo.getAffectHousehold());
                heatingExceptionInfo.setImpactTime(zHeatingExceptionInfo.getAffectDuration()+"");
                heatingExceptionInfo.setPlanReplyTime(zHeatingExceptionInfo.getEstimateTime());
                heatingExceptionInfo.setStartTime(zHeatingExceptionInfo.getOccurDatetime());
                heatingExceptionInfo.setSource(4);
                heatingExceptionInfo.setStatusCounty(1);//默认县级审核
                heatingExceptionInfo.setStatus(zHeatingExceptionInfo.getState());
                heatingExceptionInfoService.saveOrUpdate(heatingExceptionInfo);
                zHeatingExceptionInfo.setWdtIsSync(1);
                zHeatingExceptionInfoService.saveOrUpdate(zHeatingExceptionInfo);
            }
        }

    }


}
